package org.streets.eis.ext.analysis.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.streets.database.annotations.NestedFetch;
 
@SuppressWarnings("serial")
@Entity
@Table(name="eis_query")
public class QueryStruct implements Serializable {
	@Id
	private String id = "-1";
	@Column
    private String name;
	@Column
	private String memo;
	@Column
    private String owner;
	
	//Join Tables format: tableName1, tableName2
	@Column
	private String tableExpr;
	
	//条件表达式
	@Column
	private String conditionExpr;	
	
    @Column
    private String sql;

	private List<JoinTable> tables = new ArrayList<JoinTable>();

	@NestedFetch(
			type = WhereField.class,
			value= "select * from eis_query_where where query_id = :id"
			)
	private List<WhereField> whereFields = new ArrayList<WhereField>();

	private List<String> conditions = new ArrayList<String>();

	@NestedFetch(
			type = DisplayField.class,
			value= "select * from eis_query_disp where query_id = :id"		
			)	
	private List<DisplayField> displayFields = new ArrayList<DisplayField>();
	
	private List<String> idFields = new ArrayList<String>();
	
	
	@NestedFetch(
			type = OrderField.class,
			value= "select * from eis_query_order where query_id = :id"
			)		
	
	private List<OrderField> orderFields = new ArrayList<OrderField>();
    	
	public QueryStruct() {

	}

	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public String getMemo() {
		return memo;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}

	public String getOwner() {
		return owner;
	}

	//设计预案：将sql语句解析为各个域？？？？
	public void setSql(String sql) {
		this.sql = sql;
	}

	public String getSql() {
		return sql;
	}

	public void setWhereFields(List<WhereField> whereFields) {
		this.whereFields = whereFields;
	}

	public List<WhereField> getWhereFields() {
		return whereFields;
	}

	public void setDisplayFields(List<DisplayField> displayFields) {
		this.displayFields = displayFields;
	}

	public List<DisplayField> getDisplayFields() {
		return displayFields;
	}

	public void setOrderFields(List<OrderField> orderFields) {
		this.orderFields = orderFields;
	}

	public List<OrderField> getOrderFields() {
		return orderFields;
	}

	public void setConditionExpr(String expr) {
		this.conditionExpr = expr;
	}

	public String getConditionExpr() {
		return conditionExpr;
	}
	
    public String getTableExpr() {
		return tableExpr;
	}

	public void setTableExpr(String tableNames) {
		this.tableExpr = tableNames;
	}

	@Override
    public boolean equals(Object object) {
        if (!(object instanceof QueryStruct)) {
            return false;
        }
        QueryStruct other = (QueryStruct) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equalsIgnoreCase(other.id))) {

            return false;
        }
        return true;
    }

	
	public void setTables(List<JoinTable> tables) {
		this.tables = tables;
	}

	public List<JoinTable> getTables() {
		return tables;
	}

	public boolean containsTable(String tableName) {
		for(JoinTable joinTable : tables){
			if (joinTable.getTableDef().getName().equalsIgnoreCase(tableName)){
				return true;
			}			
		}

		return false;
	}

	public String[] getTableNames() {
		String[] tableNames = new String[tables.size()];
		for(int i=0; i < tables.size() ; i++){
			tableNames[i] = tables.get(i).getTableDef().getName();
		}

		return tableNames;
	}

	public void setConditions(List<String> conditions) {
		this.conditions = conditions;
	}

	public List<String> getConditions() {
		return conditions;
	}

	public void setIdFields(List<String> idFields) {
		this.idFields = idFields;
	}

	public List<String> getIdFields() {
		return idFields;
	}
	
	
}
